<!-- 车辆管理 -->
<template>
  <div class="dashboard-container driver customer-list-box">
    <div class="app-container">
      <el-card shadow="never">
        <el-form
          ref="driverSearchForm"
          :model="driverSearchForm"
          label-width="100px"
        >
          <el-col :span="8">
            <el-form-item
              label="司机账号："
              prop="account"
            >
              <el-input
                v-model="driverSearchForm.account"
                placeholder="请输入司机账号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="司机姓名："
              prop="name"
            >
              <el-input
                v-model="driverSearchForm.name"
                placeholder="请输入司机姓名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="司机手机号："
              prop="phone"
            >
              <el-input
                v-model="driverSearchForm.phone"
                placeholder="请输入司机手机号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="所属机构："
              prop="agentId"
            >
              <el-select
                v-model="driverSearchForm.agentId"
                placeholder="请选择所属机构"
              >
                <el-option
                  v-for="item in driverAgentList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item style="margin-left: -78px;">
              <el-button
                type="warning"
                @click="searchDriver"
              >搜索</el-button>
              <el-button @click="resetSearch">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-card>
      <el-card
        shadow="never"
        style="margin-top: 30px;"
      >
        <el-table
          :data="driverList"
          style="width: 100%;"
        >
          <el-table-column
            type="index"
            label="序号"
            width="100"
          ></el-table-column>
          <el-table-column
            label="司机账号"
            prop="account"
            width="300"
          ></el-table-column>
          <el-table-column
            label="司机姓名"
            prop="name"
            width="250"
          ></el-table-column>
          <el-table-column
            label="司机电话"
            prop="mobile"
            width="300"
          ></el-table-column>
          <el-table-column
            label="关联车辆"
            prop="truck.licensePlate"
            width="250"
          >
          </el-table-column>
          <el-table-column
            label="所属机构"
            prop="agency.name"
            width="250"
          ></el-table-column>
          <el-table-column
            label="工作状态"
            prop="workStatus"
            width="250"
          >
            <template slot-scope="scope">
              <span
                v-if="scope.row.workStatus == 0"
                class="tableColumn-status stop-use"
              >休息</span>
              <span
                v-else-if="scope.row.workStatus == 1"
                class="tableColumn-status"
              >上班</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="vewDetail(scope.row.userId)"
              >查看</el-button>
              <span>|</span>
              <el-button
                size="mini"
                type="text"
                @click="openCar(scope.row)"
              >配置车辆</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="driverSearchForm.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="driverSearchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="counts"
          style="margin-top: 20px;text-align: center;"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </el-card>
    </div>
    <el-dialog
      title="配置司机"
      :visible.sync="carDialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-card
        shadow="never"
        style="background-color: #F7F8F9;"
      >
        <el-form>
          <el-col :span="12">
            <el-form-item label="司机编号">
              <span>{{ carForm.userId }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机名称">
              <span>{{ carForm.name }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机电话">
              <span>{{ carForm.mobile }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属机构">
              <!-- <span>{{ carForm.agency.name }}</span> -->
            </el-form-item>
          </el-col>
        </el-form>
      </el-card>
      <el-form
        label-width="80px"
        style="margin-top: 20px;"
        :model="chooseCarForm"
      >
        <el-form-item label="配置车辆">
          <el-select v-model="chooseCarForm.truckId">
            <el-option
              v-for="item in chooseCar"
              :key="item.id"
              :value="item.id"
              :label="item.licensePlate"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="warning"
          @click="getCarAndDriver"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { agencyList } from '@/api/institutions'
import { driverList, getTruckListInDriver, driverArrangeCar } from '@/api/transit'
export default {
  name: 'Courses',
  data () {
    return {
      driverSearchForm: {
        account: '',
        agentId: null,
        name: '',
        phone: '',
        workStatus: '',
        page: 1,
        pageSize: 10
      },
      driverAgentList: [],
      driverList: [],
      counts: 0,
      carDialogVisible: false,
      carForm: {},
      chooseCar: [],
      chooseCarForm: {
        driverIds: [],
        truckId: ''
      }
    }
  },
  mounted () {
    this.getDriverAgentList()
    this.getDriverList()
    this.getTruckListInDriver()
  },
  methods: {
    async getDriverAgentList() {
      const res = await agencyList()
      this.driverAgentList = JSON.parse(res.data)
    },
    async getDriverList() {
      const res = await driverList(this.driverSearchForm)
      this.driverList = res.data.items
      this.counts = +res.data.counts
    },
    handleSizeChange(val) {
      this.driverSearchForm.pageSize = val
      this.getDriverList()
    },
    handleCurrentChange(val) {
      this.driverSearchForm.page = val
      this.getDriverList()
    },
    searchDriver() {
      this.getDriverList()
    },
    resetSearch() {
      this.$refs.driverSearchForm.resetFields()
      this.getDriverList()
    },
    vewDetail(id) {
      this.$router.push(`/transit/driver-detail?id=${id}`)
    },
    openCar(row) {
      const h = this.$createElement
      this.$msgbox({
        title: '配置车辆',
        message: h('div', null, [h('p', null, '配置车辆需要满足以下条件：'), h('p', null, '①司机信息已完善   ②司机已设置排班   ③司机绑定车辆未在启用状态')]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            this.carDialogVisible = true
            this.carForm = row
            this.chooseCarForm.driverIds = [
              ...row.userId
            ]
            done()
          } else {
            done()
          }
        }
      })
    },
    async getTruckListInDriver() {
      const res = await getTruckListInDriver()
      this.chooseCar = res.data
    },
    handleClose() {
      this.chooseCarForm = {
        driverIds: [],
        truckId: ''
      }
      this.carDialogVisible = false
    },
    async getCarAndDriver() {
      const res = await driverArrangeCar(this.chooseCarForm)
      if (res.code === 200) {
        this.$message.success('车辆配置成功')
        this.handleClose()
      } else {
        this.$message.warning(res.msg)
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
// 下拉框
::v-deep .el-select {
  width: 100%;
}
</style>

